package com.RPGDice.util;

import com.criticalritrpg.*;

import android.content.Context;

public class Settings {
	
	
	
	public static final int MIN_ROLLS = 1;
	public static final int MAX_ROLLS = 20;
	
	private static int numberOfDices[] = {1, 3, 0, 0, 1, 0};
	
	private static int numberOfRolls = 2;
	private static int successMargin[] = {0, 50};
	private static boolean showSum = true;
	private static boolean showSuccess = true;
	
	private static String historyResults = "";
	private static int roundCount = 0;
	
	public static void setDiceValues(int d4, int d6, int d8, int d10, int d12, int d20){
		if (d4 >= Dices.MIN_DICE_NUM && d4 <= Dices.MAX_DICE_NUM) numberOfDices[Dices.DICE_D4] = d4;
		if (d6 >= Dices.MIN_DICE_NUM && d6 <= Dices.MAX_DICE_NUM ) numberOfDices[Dices.DICE_D6] = d6;
		if (d8 >= Dices.MIN_DICE_NUM && d8 <= Dices.MAX_DICE_NUM ) numberOfDices[Dices.DICE_D8] = d8;
		if (d10 >= Dices.MIN_DICE_NUM && d10 <= Dices.MAX_DICE_NUM ) numberOfDices[Dices.DICE_D10] = d10;
		if (d12 >= Dices.MIN_DICE_NUM && d12 <= Dices.MAX_DICE_NUM ) numberOfDices[Dices.DICE_D12] = d12;
		if (d20 >= Dices.MIN_DICE_NUM && d20 <= Dices.MAX_DICE_NUM ) numberOfDices[Dices.DICE_D20] = d20;
	}
	
	public static int[] getNumberOfDices(){
		return numberOfDices;
	}
	
	public static int getNumOfRolls(){
		return numberOfRolls;
	}
	
	public static void setNumOfRolls(int number){
		if (number >= MIN_ROLLS && number <= MAX_ROLLS)
			numberOfRolls = number;
	}
	
	public static boolean getShowSum(){
		return showSum;
	}
	
	public static void setShowSum(boolean show){
		showSum = show;
	}
	
	public static boolean getShowSuccess(){
		return showSuccess;
	}
	
	public static void setShowSuccess(boolean show){
		showSuccess = show;
	}
	
	public static int[] getSuccessMargin(){
		return successMargin;
	}
	
	public static boolean checkValidMargins(){
		int minVal = 0, maxVal = 0;
		for (int i = 0; i < Dices.DICES_MAX_VALUE.length; i++){
			minVal += numberOfDices[i] * Dices.DICES_MIN_VALUE[i];
			maxVal += numberOfDices[i] * Dices.DICES_MAX_VALUE[i];
		}
		minVal *= numberOfRolls;
		maxVal *= numberOfRolls;
		
		return (getSuccessMargin()[0] <= maxVal && getSuccessMargin()[1] >= minVal && getSuccessMargin()[0] <= getSuccessMargin()[1]);
	}
	
	public static int checkMargins(int lower, int upper){
		int minVal = 0, maxVal = 0;
		if (lower > upper) return -1;
		for (int i = 0; i < Dices.DICES_MAX_VALUE.length; i++){
			minVal += numberOfDices[i] * Dices.DICES_MIN_VALUE[i];
			maxVal += numberOfDices[i] * Dices.DICES_MAX_VALUE[i];
		}
		minVal *= numberOfRolls;
		maxVal *= numberOfRolls;
		if ( lower > maxVal) return -2;
		if ( upper < minVal) return -3;
		
		return 0;
	}
	
	public static void setSuccessMargin(int lower, int upper){
		successMargin[0] = lower;
		successMargin[1] = upper;
	}
	
	public static void addToHistory(String result, Context cont){
		roundCount ++;
		String newText = "<font color=" + "#000000" + ">" + "<b> " + cont.getString(R.string.round_number) + roundCount + " ================" + " </b>" + "</font>"  + "<br />";
		historyResults += newText + result;
	}
	
	public static String getHistory(){
		return historyResults;
	}
	
	public static void clearHistory(){
		historyResults = "";
	}

}
